/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.*;
import java.util.Date;

/**
 * <p>Project: javase - ObjectStream1
 * <p>Powered by webrx On 2021-12-15 18:49:52
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ObjectStream1 {
    public static void main(String[] args) {
        try (var ois = new ObjectInputStream(new FileInputStream("d:/ddd.dat"))) {
            var o = ois.readObject();
            //System.out.println(o instanceof Date);
            Date a = (Date) o;
            System.out.printf("%tF %1$tT", a);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public static void write(String[] args) {
        var d = new Date();
        //把对象d保存到文件中

        //ObjectInputStream 对象输入流，可以读取对象
        //ObjectOutputStream 对象输出流，可以保存到对象到文件中
        //对象流操作的对象，必须实现序列化
        //什么是序列化？什么是反序列化？java程序中如何序列化？

        try (var oop = new ObjectOutputStream(new FileOutputStream("d:/ddd.dat"))) {
            oop.writeObject(d);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (var oop = new ObjectOutputStream(new FileOutputStream("d:/ddd.dat"))) {
            oop.writeObject(d);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
